/****************************************************************************
**
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
//#include "widget.h"



#include "console.h"
#include <QScrollBar>
#include <QMessageBox>
#include <QtCore/QDebug>
#include <QLabel>
#include <QDateTimeEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QString>
#include<QDebug>
#include<QPlainTextEdit>
#include<QTableWidgetItem>
#include<QTextCodec>
#include "stare.h"
#include<QTextStream>
#include "settingsdialog.h"
Console::Console(QWidget *parent)
  : QTableWidget(parent)
    , localEchoEnabled(false)
{
                setColumnCount(6);
                setStyleSheet("QHeaderView::section{background:skyblue;}");
                setSelectionBehavior(QAbstractItemView::SelectRows);
                setAlternatingRowColors(true);

}
//! [0]
void Console::struct_Size(int    file_sum)
{  
    RowCount   =   file_sum;
    setRowCount(RowCount);
}

void    Console::path_save(QString   file)
{
    File_Path   =   file;
}


void    Console::Name_file_read(QString  Name_read)
{
    Name_path   =   Name_read;
}
void    Console::Max_file_read(QString  Max_read)
{
    Max_path    =   Max_read;
}

void    Console::Min_file_read(QString  Min_read)
{
    Min_path    =   Min_read;
}
void    Console::Read_file_read(QString  Read_date)
{
    Read_path    =   Read_date;
}


void    Console::Sex_file_read(QString  Sex_read)
{
    Sex_path   =   Sex_read;
}

//! [1]
void Console::Txt_Data()
{
    QStringList header;
    header.append(QObject::tr("name"));
    header.append(QObject::tr("sex"));
    header.append(QObject::tr("min"));
    header.append(QObject::tr("max"));
    header.append(QObject::tr("val"));
    header.append(QObject::tr("test"));
    setHorizontalHeaderLabels(header);
    QString List_name[RowCount+1];
    QString List_sex[RowCount+1];
    QString List_max[RowCount+1];
    QString List_min[RowCount+1];
    QString list[RowCount+1];
    QString Buf[RowCount+1];
    QFile   open_name_file(Name_path);
    if(!open_name_file.open(QIODevice::ReadOnly))
    {
       QMessageBox::about(NULL,"Test Simple Terminal ","<font color='red'>not  find /home/boot/terminal/name_list.txt</font");

    }
   QFile   open_sex_file(Sex_path);
   if(!open_sex_file.open(QIODevice::ReadOnly))
   {
   QMessageBox::about(NULL,"Test Simple Terminal ","<font color='red'>not  find /home/boot/terminal/sex_list.txt</font");
   }
   QFile   open_max_file(Max_path);
   if(!open_max_file.open(QIODevice::ReadOnly))
   {
   QMessageBox::about(NULL,"Test Simple Terminal ","<font color='red'>not  find /home/boot/terminal/sex_list.txt</font");
   }
   QFile   open_min_file(Min_path);
   if(!open_min_file.open(QIODevice::ReadOnly))
   {
       QMessageBox::about(this, tr("About file open errer"),
                          tr("not path  min_list.txt"
                             "/home/boot/terminal/min_list.txt "
                             "file read errer"));
   }
   QVector<QString>RowCount_name;
    while(!open_name_file.atEnd())
    {

        QString lineString_x=QString    (open_name_file.readLine()).trimmed();
        RowCount_name<<(i++,lineString_x);

    }
    for(int i   =   0;i <   RowCount_name.count();i++)
    {
    List_name[i]    =   QString("%1").arg(RowCount_name[i]);;
    }
        QVector<QString>RowCount_sex;
    while(!open_sex_file.atEnd())
    {

        QString lineString_x    =   QString    (open_sex_file.readLine()).trimmed();
        RowCount_sex<<(i++,lineString_x);

    }
    for(int i   =   0;i <   RowCount_sex.count();i++)
    {
    List_sex[i] =   QString("%1").arg(RowCount_sex[i]);
    }


        QVector<QString>RowCount_max;

    while(!open_max_file.atEnd())
    {

        QString lineString_x    =   QString    (open_max_file.readLine()).trimmed();
        RowCount_max<<(i++,lineString_x);
    }
    for(int i   =   0;i <   RowCount_max.count();i++)
    {
    List_max[i] =   QString("%1").arg(RowCount_max[i]);;
    }
    QVector<QString>RowCount_min;

while(!open_min_file.atEnd())
{

    QString lineString_x    =   QString    (open_min_file.readLine()).trimmed();
    RowCount_min<<(i++,lineString_x);


}
for(int i   =   0;i <   RowCount_min.count();i++)
{
List_min[i] =   QString("%1").arg(RowCount_min[i]);;
}




        QFile   open_test_date_file(Read_path);

        if(!open_test_date_file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
            QMessageBox::about(this, tr("About file open errer"),
                               tr("not path  output.txt"
                                  "/home/boot/terminal/output.txt "
                                  "file read not"));
        }
      QVector<QString>RowCount_Test;
      while(!open_test_date_file.atEnd())
      {

            QString line_x  =   QString(open_test_date_file.readLine()).trimmed();
            RowCount_Test<<(i++,line_x);

      }
    double    X_min,Y_max,z_sum;
    double    buf[RowCount+1];
    double    min[RowCount+1];
    double    max[RowCount+1];
    for(I   =   0;I <   RowCount_Test.count();I++)
        {
        list[I] =   QString("%1").arg(RowCount_Test[I]);
       // list[I] ->   QString("%1").arg(RowCount_Test[I]);

        Buf[I]  =   list[I];

     if(list[I] ==   "pass")
     {
            pass_las++;
            p_name  =   pass_las+0;
            Send_pass();
            Send_pass_windos();

     }
    else
       if(list[I]   ==  "fall")
       {
            fall_las++;
            f_name  =   fall_las+0;
            Send_fall();
            Send_fall_windos();

    }
    buf[I]  =   list[I].toDouble();
    min[I]  =   List_min[I].toDouble();
    max[I]  =   List_max[I].toDouble();
    X_min  =   min[I];
    Y_max  =   max[I];
    z_sum  =   buf[I];

    QTableWidgetItem    *name=new   QTableWidgetItem(List_name[I]);
    QTableWidgetItem    *sex=new   QTableWidgetItem(List_sex[I]);
    QTableWidgetItem    *min=new   QTableWidgetItem(List_min[I]);
    QTableWidgetItem    *max=new   QTableWidgetItem(List_max[I]);
    QTableWidgetItem    *value=new   QTableWidgetItem(list[I]);
    QTableWidgetItem    *fall=new   QTableWidgetItem("fall");
    QTableWidgetItem    *pass=new   QTableWidgetItem("pass");
   if((z_sum  <=  Y_max)&(z_sum >=  X_min))
        {
name->setBackgroundColor(QColor(115,210,22));
sex->setBackgroundColor(QColor(115,210,22));
min->setBackgroundColor(QColor(115,210,22));
max->setBackgroundColor(QColor(115,210,22));
value->setBackgroundColor(QColor(115,210,22));
pass->setBackgroundColor(QColor(115,210,22));
setItem(I,0,name);
setItem(I,1,sex);
setItem(I,2,min);
setItem(I,3,max);
setItem(I,4,value);
setItem(I,5,pass);

name->setTextAlignment(Qt::AlignCenter);
sex->setTextAlignment(Qt::AlignCenter);
min->setTextAlignment(Qt::AlignCenter);
max->setTextAlignment(Qt::AlignCenter);
name->setTextAlignment(Qt::AlignCenter);
value->setTextAlignment(Qt::AlignCenter);
pass->setTextAlignment(Qt::AlignCenter);
   }
      else
        {

       name->setBackgroundColor(QColor(255,60,10));
       sex->setBackgroundColor(QColor(255,60,10));
       min->setBackgroundColor(QColor(255,60,10));
       max->setBackgroundColor(QColor(255,60,10));
       value->setBackgroundColor(QColor(255,60,10));
       fall->setBackgroundColor(QColor(255,60,10));
       setItem(I,0,name);
       setItem(I,1,sex);
       setItem(I,2,min);
       setItem(I,3,max);
       setItem(I,4,value);
       setItem(I,5,fall);
       name->setTextAlignment(Qt::AlignCenter);
       sex->setTextAlignment(Qt::AlignCenter);
       min->setTextAlignment(Qt::AlignCenter);
       max->setTextAlignment(Qt::AlignCenter);
       name->setTextAlignment(Qt::AlignCenter);
       value->setTextAlignment(Qt::AlignCenter);
       fall->setTextAlignment(Qt::AlignCenter);

         }

}
p_number    =   I;
progress();

}

//! [2]
void Console::setLocalEchoEnabled(bool set)
{
    localEchoEnabled = set;
}
//! [3]
void Console::mousePressEvent(QMouseEvent *e)
{
    Q_UNUSED(e)
    setFocus();
}
//! [4]
void Console::mouseDoubleClickEvent(QMouseEvent *e)
{
    Q_UNUSED(e)
}
//! [5]
void Console::contextMenuEvent(QContextMenuEvent *e)
{
    Q_UNUSED(e)
}
//! [6]
void Console::resizeEvent(QResizeEvent *event)
{
    for(int I   =   0;I <   RowCount;I++)
    setRowHeight(I,25);
    for(int I   =   0;I <   columnCount();I++)
    setColumnWidth(I,event->size().width()/columnCount());
    QTableWidget::resizeEvent(event);

}
